<template>
	<view class="container">		
		<!-- <view class="intro">本项目已包含uni ui组件，无需import和注册，可直接使用。在代码区键入字母u，即可通过代码助手列出所有可用组件。光标置于组件名称处按F1，即可查看组件文档。</view>
		<text class="intro">详见：</text>
		 -->
		<!-- 波特率baudRate:{{gSettings.baudRate}}
		数据位dataBits:{{gSettings.dataBits}}
		停止位stopBits:{{gSettings.stopBits}}
		校验parity:{{gSettings.parity}}-->
		<uni-popup ref="messagePopup" type="message">
			<uni-popup-message type="error" :duration="0">
				<text class="message">
					<uni-icons type="settings" color="orangered"></uni-icons>  请插入串口设备
				</text>
			</uni-popup-message>
		</uni-popup> 
		<uni-list>
			<uni-list-item title="devSerial/usb11"  showArrow clickable  @click="(e)=>openSerial('devSerial/usb1')"></uni-list-item>
			<uni-list-item title="devSerial/usb12"  showArrow clickable @click="(e)=>openSerial('devSerial/usb12')"></uni-list-item>
		</uni-list>
		<view style="width:100%;word-break:break-all">{{usbInfo}}</view>		
	</view>
</template>

<script setup>
	import { ref,onBeforeMount,onMounted } from 'vue'
	import { onNavigationBarButtonTap } from '@dcloudio/uni-app'  
	import { useGSettingsStore } from '@/store'
	import {dbHelper,InitDatabase} from '@/dataAccess/index.js'
	
	//const usbSerial = uni.requireNativePlugin("usb-serial-for-uniapp");
	//const globalEvent = uni.requireNativePlugin('globalEvent');
	const messagePopup=ref(null);
	const gSettings = useGSettingsStore()
	const usbInfo=ref('')
	const openSerial=(usb)=>{
		console.log('openSerial',usb)
		uni.navigateTo({
			url: `/pages/main/main?usb=${usb}`,
		});
	}
	
	onNavigationBarButtonTap((e)=>{
		console.log('index onNavigationBarButtonTap',e)
		if(e.index === 0){
			uni.navigateTo({
				url: '/pages/settings/settings'
			});
		}
	});
	
	onBeforeMount(()=>{
		InitDatabase();
		// globalEvent.addEventListener('usbListener', (e)=>{
		//   console.log('usbListener',e)
		//   try{
		// 	  usbInfo.value=usbInfo.value+'/r/n \r\n usbListener:'+JSON.stringify(e)
		//   }catch{
			  
		//   }
		  
		// });
	});
	
	onMounted(()=>{		
		// var result = usbSerial.getUsbDevices();
		// console.log('getUsbDevices',result)
		// usbInfo.value=JSON.stringify(result);
		// if(result.code == 500){
		// 	//messagePopup.value.open();
		// }
	});
</script>

<style>
	.container {
		padding: 0px;
		font-size: 14px;
		line-height: 24px;
	}
	
	.message{
		color:orangered;
	}
</style>
